package com.example.wan_android.adapter


import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.core.CoreViewHolder
import com.example.wan_android.databinding.AdapterTabBinding
import com.example.wan_android.model.TabItemInfoData
import com.example.wan_android.viewmodel.MainViewModel
import com.example.wan_android.viewmodel.TabAdapterViewModel

class TabAdapter(vm: MainViewModel) :
    CoreAdapter<AdapterTabBinding, MainViewModel, TabItemInfoData>(
        BR.data,
        BR.viewModel,
        vm
    ) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_tab
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        Glide.with(holder.itemView.context)
            .load(mListData[position].tabIcon)
            .into(holder.binding.root.findViewById<androidx.appcompat.widget.AppCompatImageView>(R.id.iv_tab_icon))
        holder.binding.root.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(R.id.cl_item)
            .setOnClickListener({
                mViewModel?.setIndex(position)
                mViewModel?.setIndexTitle(mListData[position].tabName)
                notifyDataSetChanged()
            })
        if (mViewModel?.indexLiveData?.value == position) {
            holder.binding.root.findViewById<TextView>(R.id.tv_name)
                .setTextColor(
                    ContextCompat.getColor(
                        holder.itemView.context,
                        R.color.color_orange_deep
                    )
                )
        } else {
            holder.binding.root.findViewById<TextView>(R.id.tv_name)
                .setTextColor(
                    ContextCompat.getColor(
                        holder.itemView.context,
                        R.color.color_5d5f5e
                    )
                )
        }
    }


}